#include "optionswin.h"
#include "ui_optionswin.h"
#include "mainobject.h"

OptionsWin::OptionsWin(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::OptionsWin)
{
    m_ui->setupUi(this);
}

OptionsWin::~OptionsWin()
{
    delete m_ui;
}

void OptionsWin::changeEvent(QEvent *e){
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void OptionsWin::keyPressEvent(QKeyEvent *evt){
    if(evt->key()==Qt::Key_Escape){
        evt->ignore();
#ifdef Q_OS_MAC
        cancelClick();
#endif
    }
}

void OptionsWin::load(){
    QSettings settings;
    QStringList notebooks = MainObject::instance()->getNotebooks();
    m_ui->nbText->clear();
    m_ui->nbText->addItems(notebooks);
    int pos = notebooks.indexOf(settings.value(EN_TEXT_NB).toString());
    if(pos==-1)
        pos = 0;
    m_ui->nbText->setCurrentIndex(pos);

    m_ui->nbFile->clear();
    m_ui->nbFile->addItems(notebooks);
    pos = notebooks.indexOf(settings.value(EN_FILE_NB).toString());
    if(pos==-1)
        pos = 0;
    m_ui->nbFile->setCurrentIndex(pos);
    m_ui->tagText->setText(settings.value(EN_TEXT_TAGS).toString());
    m_ui->tagFile->setText(settings.value(EN_FILE_TAGS).toString());
    m_ui->activeOpacity->setValue(settings.value(EN_ACTIVE_OPACITY, EN_ACTIVE_OPACITY_DEF).toInt());
    m_ui->inactiveOpacity->setValue(settings.value(EN_INACTIVE_OPACITY, EN_INACTIVE_OPACITY_DEF).toInt());
    m_ui->locale->setCurrentIndex(settings.value(EN_LOCALE, 0).toInt());
    m_ui->onTop->setChecked(settings.value(EN_ONTOP, EN_ONTOP_DEF).toBool());
    m_ui->isPremium->setChecked(settings.value(EN_PREMIUM, EN_PREMIUM_DEF).toBool());

#ifdef Q_OS_WIN32
    m_ui->enLocation->
            setText(settings.value(EN_LOCATION).toString());
#endif
    m_ui->hotkey1->setText(settings.value(EN_TEXT_HOTKEY, EN_TEXT_HOTKEY_DEF).toString());
}

bool OptionsWin::save(){
    QSettings settings;

    MainObject::instance()->disconnectHotkeys();

    settings.setValue(EN_TEXT_NB, m_ui->nbText->currentText());
    settings.setValue(EN_FILE_NB, m_ui->nbFile->currentText());
    settings.setValue(EN_TEXT_HOTKEY, m_ui->hotkey1->text());
    settings.setValue(EN_TEXT_TAGS, m_ui->tagText->text());
    settings.setValue(EN_FILE_TAGS, m_ui->tagFile->text());
    settings.setValue(EN_ACTIVE_OPACITY, m_ui->activeOpacity->value());
    settings.setValue(EN_INACTIVE_OPACITY, m_ui->inactiveOpacity->value());
    settings.setValue(EN_LOCALE, m_ui->locale->currentIndex());
    settings.setValue(EN_ONTOP, m_ui->onTop->isChecked());
    settings.setValue(EN_PREMIUM, m_ui->isPremium->isChecked());
    bool isVisible = MainObject::instance()->fieldWin->isVisible();
    MainObject::instance()->fieldWin->applyWindowFlags();
    if(isVisible){
        MainObject::instance()->fieldWin->hide();
        MainObject::instance()->toggleTextField(true);
    }
//    MainObject::instance()->fieldWin->wind
    MainObject::instance()->connectHotkeys();
#ifdef Q_OS_WIN32
    settings.setValue(EN_LOCATION, m_ui->enLocation->text());
    QString enScript = settings.value(EN_LOCATION).toString()+EN_EXECUTABLE;
    QFile file(enScript);
    if(!file.exists()){
        QMessageBox::critical(this, tr("Evernote trap"),
                              tr("Can't find Evernote in specified directory"));
        MainObject::instance()->enFound = false;
        return false;
    }
    MainObject::instance()->enFound = true;
    MainObject::instance()->enLine = enScript;
#endif
    return true;
}

void OptionsWin::show(){
    QDialog::show();
#ifdef Q_OS_DARWIN
    m_ui->enLocation->setEnabled(false);
    m_ui->enLocationSet->setEnabled(false);
#endif
    load();
}

void OptionsWin::okClick(){
    if(save())
        hide();
}

void OptionsWin::applyClick(){
    save();
    load();
}

void OptionsWin::cancelClick(){
    hide();
}

void OptionsWin::editHotkey1(){
    MainObject::instance()->hotkeyEditor->show(m_ui->hotkey1->text());
}

void OptionsWin::hotkeySet(QString key){
    m_ui->hotkey1->setText(key);
}

void OptionsWin::editLocation(){
    QString dir = QFileDialog::getExistingDirectory(this, tr("Select Evernote location:"));
    if(dir!=""){
        m_ui->enLocation->setText(dir);
    }
}
